package org.fing.edu.uy.esbadp.motor.config;

import java.io.File;
import java.net.URL;
import java.util.logging.Level;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.fing.edu.uy.esbadp.core.constants.ConfigConstants;
import org.fing.edu.uy.esbadp.core.entity.DMAdpAdaptationRequirements;
import org.fing.edu.uy.esbadp.core.entity.DMAdpEvents;
import org.fing.edu.uy.esbadp.core.entity.DMAdpStrategies;
import org.fing.edu.uy.esbadp.core.entity.DMAdpStrategy;
import org.fing.edu.uy.esbadp.logger.LoggerADP;
import org.fing.edu.uy.esbadp.motor.register.RegistryManager;

public class LoaderXMLConfig {
	
	private static java.util.logging.Logger logger = LoggerADP.getLogger(LoaderXMLConfig.class.getSimpleName());
	
	public void loadMonitoredEvents() {
		logger.info("Loading Adaptative Monitored Events...");
		try {
			String path = ConfigConstants.EVENTS_DEF_PATH ;  			
			URL url = new URL(path);  			
			File actiondef = new File(url.toURI());		
			if(actiondef.exists()) {  				
				JAXBContext context = JAXBContext.newInstance(DMAdpEvents.class);
				Unmarshaller u = context.createUnmarshaller();
				DMAdpEvents adpEvents = (DMAdpEvents) u.unmarshal(actiondef);			
				logger.info(adpEvents.toString());
				
				//Seteo los requerimientos de servicios cargados
				RegistryManager.getInstance().setAdpEvents(adpEvents);
			} else {  
				logger.log(Level.SEVERE, "Strategies definition file not found ( " + ConfigConstants.EVENTS_DEF_PATH  + ")");
			}  
		} catch (Exception ex) {
			logger.log(Level.SEVERE, "Exception registering adp Events.\nSee StackTarce.", ex);
		}
	}
	
	public void loadAdaptationRequirements() {
		logger.info("Loading Adaptative Service Requirements");
		try {
			String path = ConfigConstants.ADAPTATION_REQUIREMENTS_DEF_PATH ;  			
			URL url = new URL(path);  			
			File actiondef = new File(url.toURI());		
			if(actiondef.exists()) {  				
				JAXBContext context = JAXBContext.newInstance(DMAdpAdaptationRequirements.class);
				Unmarshaller u = context.createUnmarshaller();
				DMAdpAdaptationRequirements adpAdpaptationRequirements = (DMAdpAdaptationRequirements) u.unmarshal(actiondef);			
				logger.info(adpAdpaptationRequirements.toString());
				//Seteo los requerimientos de servicios cargados
				RegistryManager.getInstance().setAdpRequirements(adpAdpaptationRequirements);
			} else {  
				logger.log(Level.SEVERE, "Strategies definition file not found ( " + ConfigConstants.ADAPTATION_REQUIREMENTS_DEF_PATH  + ")");
			}  
		
		} catch (Exception ex) {
			logger.log(Level.SEVERE, "Exception registering adp Requirements.\nSee StackTarce.", ex);
		}
	}
	
	public void loadStrategies() {
		logger.info("Loading Adaptative Strategies");
		try {
			String path = ConfigConstants.STRATEGIES_DEF_PATH ;  			
			URL url = new URL(path);  			
			File actiondef = new File(url.toURI());		
			if(actiondef.exists()) {  				
				JAXBContext context = JAXBContext.newInstance(DMAdpStrategies.class);
				Unmarshaller u = context.createUnmarshaller();
				DMAdpStrategies adpStrategies = (DMAdpStrategies) u.unmarshal(actiondef);			
				logger.info(adpStrategies.toString());
				
				//Seteo las estrategias cargadas.
				RegistryManager.getInstance().setAdpStrategies(adpStrategies);
				
				//Pruebo que tengo visibilidad a todas las clases. ( ver esto si lo dejamos )
				for(DMAdpStrategy aux : adpStrategies.getStrategies()){
					Class.forName(aux.getClassName());					
				}
			} else {  
				logger.log(Level.SEVERE, "Strategies definition file not found ( " + ConfigConstants.STRATEGIES_DEF_PATH + ")");
			}  
		} catch (Exception ex) {
			logger.log(Level.SEVERE, "Exception registering adp Strategies.\nSee StackTarce.", ex);
		}
	}

}
